using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace OR.Lazarus.Utility
{
	/// <summary>
	/// Summary description for FileGenerator.
	/// </summary>
	public class FileUtility
	{
		public static Stream ReadFile( string pFilename )
		{
			FileStream		vFileStream		= new FileStream( pFilename, FileMode.Open, FileAccess.Read );

			return ( vFileStream );
		}
		
		public static long WriteStream( Stream pTarget, XPathNavigator pData, XslCompiledTransform pTransform )
		{
			return ( FileUtility.WriteStream( pTarget, pData, pTransform, null ) );
		}

        public static long WriteStream(Stream pTarget, XPathNavigator pData, XslCompiledTransform pTransform, XsltArgumentList pArgs)
		{
			long				vLength			= 0;
			StreamWriter	vStreamWriter	= new StreamWriter( pTarget );
			
			try
			{
				pTransform.Transform( pData, pArgs, vStreamWriter );
			}
			catch ( Exception ex )
			{
				throw new ApplicationException( "Transform to stream failed: " + ex.Message, ex );
			}

			vLength = vStreamWriter.BaseStream.Length;

			vStreamWriter.Flush();

			return( vLength );
		}

		public static long WriteFile( String pTarget, String pSource )
		{
			StreamWriter vWriter = new StreamWriter( pTarget );

			try
			{
				string[] vLines = pSource.Split( new String[] { "\n" }, StringSplitOptions.None );

				foreach ( String vLine in vLines )
				{
					vWriter.WriteLine( vLine );
				}
			}
			finally
			{
				vWriter.Flush();
			}

			return ( pSource.Length );
		}

		public static long WriteFile( String pTarget, Stream pSource )
		{
			byte[]			vBuffer			= new byte[ pSource.Length ];
			FileStream		vFileStream		= new FileStream( pTarget, FileMode.Create, FileAccess.Write );

			pSource.Seek( 0, SeekOrigin.Begin );

			pSource.Read( vBuffer, 0, Convert.ToInt32( pSource.Length ) );

			vFileStream.Write( vBuffer, 0, Convert.ToInt32( pSource.Length ) );

			return ( pSource.Length );
		}

        public static long WriteFile(String p_szTarget, XPathNavigator p_xmlData, XslCompiledTransform p_XslCompiledTransform)
		{
			return( WriteFile( p_szTarget, p_xmlData, p_XslCompiledTransform, null ) );
		}

        public static long WriteFile(String p_szTarget, XPathNavigator p_xmlData, XslCompiledTransform p_XslCompiledTransform, XsltArgumentList p_xslalContext)
		{
			long				iLength	= 0;
			FileStream		filWrite	= new FileStream( p_szTarget, FileMode.Create, FileAccess.Write );
			StreamWriter	swWrite	= new StreamWriter( filWrite );
			
			try
			{
				p_XslCompiledTransform.Transform( p_xmlData, p_xslalContext, swWrite );
			}
			catch ( Exception ex )
			{
				throw new ApplicationException( "Transform to " + p_szTarget + " failed: " + ex.Message, ex );
			}

			iLength = swWrite.BaseStream.Length;

			swWrite.Flush();

			filWrite.Close();

			return( iLength );
		}

		public static long WriteFile( String p_szTarget, IXPathNavigable p_xmlData, XslCompiledTransform p_XslCompiledTransform )
		{
			return( WriteFile( p_szTarget, p_xmlData, p_XslCompiledTransform, null ) );
		}

		public static long WriteFile( String p_szTarget, IXPathNavigable p_xmlData, XslCompiledTransform p_XslCompiledTransform, XsltArgumentList p_xslalContext )
		{
			long				iLength	= 0;
			FileStream		filWrite	= new FileStream( p_szTarget, FileMode.Create, FileAccess.Write );
			StreamWriter	swWrite	= new StreamWriter( filWrite );
			
			p_XslCompiledTransform.Transform( p_xmlData, p_xslalContext, swWrite );

			iLength = swWrite.BaseStream.Length;

			swWrite.Flush();

			filWrite.Close();

			return( iLength );
		}

		public static long WriteFile( String p_szTarget, XmlDocument p_xmlData )
		{
			long				iLength	= 0;
			FileStream		filWrite	= new FileStream( p_szTarget, FileMode.Create, FileAccess.Write );
			StreamWriter	swWrite	= new StreamWriter( filWrite );
			
			p_xmlData.Save( swWrite );

			iLength = swWrite.BaseStream.Length;

			swWrite.Flush();

			filWrite.Close();

			return( iLength );
		}

		public static string GetPathFromFile( string p_szSource )
		{
			return( GetPathFromFile( p_szSource, true ) );
		}

		public static string GetPathFromFile( string p_szSource, bool p_bIncludeTrailingHash )
		{
			int iLastHash = p_szSource.LastIndexOf( "\\" );

			if ( p_bIncludeTrailingHash )
			{
				return( p_szSource.Substring( 0, ( iLastHash + 1 ) ) );
			}
			else
			{
				return( p_szSource.Substring( 0, iLastHash ) );
			}
		}

		public static string GetFileFromPath( string p_szSource )
		{
			int iLastHash = p_szSource.LastIndexOf( "\\" );

			return( p_szSource.Substring( iLastHash + 1 ) );
		}
	}
}
